/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dataentry;

import com.singularsys.jep.*;
import com.singularsys.jep.parser.Node;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
 *
 * @author i.capurro
 */
public class Rule {
    
    private String expression;  
    private Jep jep;    

    
    public Rule(String rule) throws ParseException {
        this.expression = rule;
        this.jep = new Jep();
        Node n1 = jep.parse(expression);   
    }
    
    public void addVariable(String var, Object o) throws JepException {
        jep.addVariable(var, o); 
        
    }
    
    public ArrayList getVariables() throws ParseException {
        ArrayList list = new ArrayList <> ();
        Iterator it = jep.getVariableTable().getVariables().iterator();
        
        while (it.hasNext()) {
            String name = ((Variable) it.next()).getName();
            if ((!name.equals("e")) && (!name.equals("pi")) && (!name.equals("false")) && (!name.equals("true")) && (!name.equals("i"))) {
                list.add(name);
            }
        }
        
        return list;
    }
    
    
    public boolean evaluate() {  
        try {
            Node n1 = jep.parse(expression);  
            Boolean result = (Boolean) jep.evaluate(n1);  
            
            return (result);
        } catch (EvaluationException | ParseException ex) {
            Logger.getLogger(Rule.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    } 
    
}
